package tools.levUtils;

import java.util.ArrayList;
import java.util.Arrays;

public class Triplet implements Comparable<Triplet>{

	String name1, name2, name3;
	int weight1, weight2, weight3;
	ArrayList<String> names = new ArrayList<String>();

	public Triplet(String name1, String name2, String name3, int weight1,
			int weight2, int weight3) {
		this.name1 = name1;
		this.name2 = name2;
		this.name3 = name3;
		this.weight1 = weight1;
		this.weight2 = weight2;
		this.weight3 = weight3;
		String[] tmp= new String[] {name1,name2,name3};
		Arrays.sort(tmp);
//		System.err.println(name1+" "+name2+" "+name3);
//		System.err.println(tmp[0]+" "+tmp[1]+" "+tmp[2]);
//		System.err.println();
		names.add(tmp[0]);
		names.add(tmp[1]);
		names.add(tmp[2]);
		
	}

	/**
	 * Assumes that name3 is the root
	 * 
	 * @param name1
	 * @param name2
	 * @param name3
	 */
	public Triplet(String name1, String name2, String name3) {
		this(name1, name2, name3, 0, 0, 1);
	}
	
	public Triplet(String name1,String name2,String name3, int weight){
		this(name1,name2,name3,0,0,weight);
	}
	
	public Triplet(Triplet b){
		this(b.name1,b.name2,b.name3,b.weight1,b.weight2,b.weight3);
	}
	
	public void add(Triplet b)throws Exception{
		if(this.compareTo(b)==0){
			if(name1.equals(b.name1)){
				weight1+=b.weight1;
				if(name2.equals(b.name2)){
					weight2+=b.weight2;
					weight3+=b.weight3;
				}else{
					weight2+=b.weight3;
					weight3+=b.weight2;
				}
			}else{
				if(name1.equals(b.name2)){
					weight1+=b.weight2;
					if(name2.equals(name1)){
						weight2+=b.weight1;
						weight3+=b.weight3;
					}else{
						weight2+=b.weight3;
						weight3+=b.weight1;
					}
				}else{
					weight1+=b.weight3;
					if(name2.equals(b.name2)){
						weight2+=b.weight2;
						weight3+=b.weight1;
					}else{
						weight2+=b.weight1;
						weight3+=b.weight2;
					}
				}
			}
		}else{
//			System.err.println(name1+" "+name2+" "+name3);
//			System.err.println(b.name1+" "+b.name2+" "+b.name3);
//			System.err.println("tried to add two triplets with different namesets");
		}
	}

	public String getNameString(){
		return names.get(0)+names.get(1)+names.get(2);
	}
	
	public String toStringMajority(boolean withWeight){
		if(weight1>weight2 && weight1>weight3){
			if(withWeight){
				return name2+" "+name3+" "+name1+" "+weight1;
			}else{
				return name2+" "+name3+" "+name1;
			}
		}else if(weight2>weight3){
			if(withWeight){
				return name1+" "+name3+" "+name2+" "+weight2;
			}else{
				return name1+" "+name3+" "+name2;
			}
		}else{
			if(withWeight){
				return name1+" "+name2+" "+name3+" "+weight3;
			}else{
				return name1+" "+name2+" "+name3;
			}
		}
	}
	
	public String toString() {
		String out = "";
		switch (weight1) {
		case 0:
			out += "";
			break;
		case 1:
			out += name2 + " " + name3 + " " + name1 + "\n";
			break;
		default:
			out += name2 + " " + name3 + " " + name1 + " " + weight1 + "\n";
			break;
		}
		switch (weight2) {
		case 0:
			out += "";
			break;
		case 1:
			out += name1 + " " + name3 + " " + name2 + "\n";
			break;
		default:
			out += name1 + " " + name3 + " " + name2 + " " + weight2 + "\n";
			break;
		}
		switch (weight3) {
		case 0:
			out += "";
			break;
		case 1:
			out += name1 + " " + name2 + " " + name3 + "\n";
			break;
		default:
			out += name1 + " " + name2 + " " + name3 + " " + weight3 + "\n";
			break;
		}
		return out;
	}

	public int compareTo(Triplet b) {
		
		for (String name : names) {
			if(!b.names.contains(name)){
//				System.err.println(name);
//				System.err.println(b.names.get(0)+" "+b.names.get(1)+" "+b.names.get(2));
				return -1;
			}
		}
		return 0;
	}
}
